﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools;
using Wpf.CommonTools.SystemExtension;

namespace Convertor
{
    /// <summary>
    /// 所有插件绑定
    /// </summary>
    public class AllPluginsViewModel : INotifyPropertyChanged
    {
        private Dictionary<string, object> allPlugins;
        /// <summary>
        /// All Original Plugins
        /// </summary>
        public Dictionary<string, object> Plugins
        {
            get { return allPlugins ?? (allPlugins = new Dictionary<string, object>()); }
            set
            {
                allPlugins = value;
                setOtherItems(value);
            }
        }
        private ObservableCollection<PluginItem> pluginsItemContainer;
        /// <summary>
        /// All Formated Plugins
        /// </summary>
        public ObservableCollection<PluginItem> PluginsItemContainer
        {
            get { return pluginsItemContainer ?? (pluginsItemContainer = new ObservableCollection<PluginItem>()); }
            set { pluginsItemContainer = value; }
        }
        //private static ObservableCollection<string> allTypes;
        ///// <summary>
        ///// All Plugin Types
        ///// </summary>
        //public static ObservableCollection<string> AllTypes
        //{
        //    get { return allTypes ?? (allTypes = new ObservableCollection<string>()); }
        //    set { allTypes = value; }
        //}

        private void setOtherItems(Dictionary<string, object> value)
        {
            PluginsItemContainer.Clear();
            var list = value.Select(p => new PluginItem(p.Key, p.Value)).OrderBy(p => p.Name).ToList();
            foreach (var item in list)
            {
                PluginsItemContainer.Add(item);
            }

            //string[] allTypes = new string[] { UILanguage.FindResource("All").ToString() }.Union((Config.AllPlugins.Plugins).Where(p => !Config.GlobalConfig.DisabledPlugins.Contains(p.Key)).Select(p => ((IPluginInfo)p.Value).Name.Split('-')[0]).GroupBy(p => p).Select(p => p.Key).OrderBy(p => p)).ToArray();
            //AllTypes.Clear();
            //foreach (string str in allTypes)
            //{
            //    AllTypes.Add(str);
            //}
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Plugins")); }
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("AllPlugins")); }
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("AllTypes")); }
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
